<?php
namespace Account\Controller;

use Common\Controller\ApiController;

class ReceiveAddressController extends ApiController
{

    public function getSelfAddress()
    {
        $input = $this->request();
        $page = (int)filter($input['page'], 1);
        $perPage = (int)filter($input['per_page'], 10);
        $addresses = D('UserAddr')->scope('valid')->where([
            'user_id' => $this->user['id'],
        ])->order('create_time desc')->select();
        $addresses = transform('ReceiveAddress', 'listview', $addresses);
        $this->response($addresses);
    }

    public function store()
    {
        $input = $this->request();

        $this->checkNull($input, ['province', 'city', 'district', 'detail', 'name', 'mobile']);
        $user = $this->user;

        $input['user_id'] = $user['user_id'];

        $count = D('UserAddr')->scope('valid')->where(['user_id' => $user['user_id']])->count();
        if ($count >= 10) {
            $this->errorResponse(30011);
        }

        $userAddrModel = D('UserAddr');
        if(isset($input['addr_id'])){
            $userAddrModel->save($input);
            $addr_id = $input['addr_id'];
        }else{
            $userAddrModel->create($input);
            $addr_id = $userAddrModel->add();
        }
        
        if ($input['is_default'] == 1) {    
                D('UserAddr')->setDefault($this->user['user_id'],$addr_id);
            }
            $addr = D('UserAddr')->find($userAddrModel->addr_id);
            $addr = transform('ReceiveAddress', 'listview', $addr);
            $this->createdResponse($addr);
    }

    public function update()
    {
        $addrId = I('get.id');
        $input = $this->request();

        $address = D('UserAddr')->scope('valid')->find($addrId);
        if ($address) {
            $input = array_keep($input, ['province', 'city', 'district', 'detail', 'name', 'mobile', 'is_default']);
            $address = array_merge($address, $input);
            $userAddrModel = D('UserAddr');
            if ($userAddrModel->create($address)) {
                $userAddrModel->save();
                if ($input['is_default'] == 1) {
                    D('UserAddr')->setDefault($this->user['user_id'], $addrId);
                }
                $address = D('UserAddr')->find($addrId);
                $address = transform('ReceiveAddress', 'listview', $address);
                $this->response($address);
            } else {
                $this->errorResponse(30000, $userAddrModel->getError());
            }
        } else {
            $this->errorResponse(30012);
        }
    }

    public function getOneAddress($id)
    {
        $address = D('UserAddr')->scope('valid')->find($id);
        $address = transform('ReceiveAddress', 'listview', $address);
        $this->response($address);
    }
    
    public function delete($id)
    {
        D('UserAddr')->where(['addr_id' => $id])->delete();
        $this->modifySuccessResponse();
    }

}
